package com.xbongbong.parent.condition.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.parent.abstracts.AbstractConditionFieldHandler;
import com.xbongbong.parent.field.FieldAnalyseService;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.enums.ComboTypeEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.help.ProFormHelp;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.FilterConditionsPoJo;
import com.xbongbong.paas.field.ItemPoJo;
import com.xbongbong.paas.field.LinkConditionPoJo;
import com.xbongbong.paas.field.LinkInfoPojo;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.util.ItemUtil;
import com.xbongbong.pro.businessrule.pojo.BusinessRuleInfoPojo;
import com.xbongbong.pro.condition.pojo.ConditionDTO;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.pro.form.pojo.RuleTargetPojo;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 *
 * @author 吴峰
 * @version v1.0
 * @date 2020/3/3 19:41
 * @since v1.0
 */
@Component
public class ConditionFormComboHandlerImpl extends AbstractConditionFieldHandler {

    @Resource
    private ProFormHelp proFormHelp;

    @Resource
    private Map<Integer, FieldAnalyseService> fieldAnalyseMap;


    @Override
    public List<Integer> fieldType() {
        return Arrays.asList(FieldTypeEnum.COMBO.getType(), FieldTypeEnum.RADIO_GROUP.getType());
    }

    @Override
    public Object handlerThisFormField(PaasFormDataEntity paasFormDataEntity, JSONObject data, FieldAttrEntity fieldAttrEntity,  FieldAttrEntity targetField,
                                       BusinessRuleInfoPojo businessRuleInfoPojo ) throws XbbException {
        Object object;
        String corpid = businessRuleInfoPojo.getCorpid();
        // 根据value获取text
        // 1、如果本表单字段是下拉框、下拉复选、单选按钮、复选按钮，需要先根据value取出text，放入data中；
        // 2、在下方取出data的text，然后拿到对应的value，之后在根据value去查询对应的数据；
        if (Objects.equals(ComboTypeEnum.CUSTOM.getType(), fieldAttrEntity.getComboType())) {
            List<ItemPoJo> itemPoJos = proFormHelp.getItem(fieldAttrEntity, businessRuleInfoPojo.getBusinessType(), corpid, paasFormDataEntity.getFormId());
            fieldAttrEntity.setItems(itemPoJos);
            object = ItemUtil.getCustomItemList(fieldAttrEntity, data);
        } else {
            object = ItemUtil.getCustomItemList(fieldAttrEntity, data);
        }
        return object;
    }

    @Override
    public Object handlerTargetFormField(PaasFormDataEntity paasFormDataEntity, JSONObject data, FieldAttrEntity fieldAttrEntity, FieldAttrEntity targetField, BusinessRuleInfoPojo businessRuleInfoPojo, FilterConditionsPoJo filterConditionsPoJo) throws XbbException {
        String corpid = businessRuleInfoPojo.getCorpid();
        RuleTargetPojo ruleTargetPojo = businessRuleInfoPojo.getRuleTargetPojo();
        Integer businessType = ruleTargetPojo.getLinkBusinessType();
        Object object = businessRuleInfoPojo.getObject();
        if (Objects.equals(ComboTypeEnum.CUSTOM.getType(), targetField.getComboType()) && Objects.nonNull(object)) {
            List<ItemPoJo> itemPoJos = proFormHelp.getItem(targetField, businessType, corpid, ruleTargetPojo.getFormId());
            targetField.setItems(itemPoJos);
            return ItemUtil.getCustomItemValue(targetField, String.valueOf(object));
        }else {
            return object;
        }
    }

    @Override
    public Object handlerFixedValue(FieldAttrEntity targetField, BusinessRuleInfoPojo businessRuleInfoPojo, FilterConditionsPoJo filterCondition) throws XbbException {
        Object object = filterCondition.getValue();
        String corpid = businessRuleInfoPojo.getCorpid();
        RuleTargetPojo ruleTargetPojo = businessRuleInfoPojo.getRuleTargetPojo();
        if (object instanceof String) {
            if (Objects.equals(ComboTypeEnum.CUSTOM.getType(), targetField.getComboType())) {
                List<ItemPoJo> itemPoJos = proFormHelp.getItem(targetField, ruleTargetPojo.getLinkBusinessType(), corpid, ruleTargetPojo.getFormId());
                targetField.setItems(itemPoJos);
                object = ItemUtil.getCustomItemValue(targetField, String.valueOf(object));
            }
        } else if (object instanceof Map || object instanceof ItemPoJo) {
            if (Objects.equals(ComboTypeEnum.CUSTOM.getType(), targetField.getComboType())) {
                ItemPoJo itemPoJo = JSON.parseObject(JSON.toJSONString(object), ItemPoJo.class);
                object = itemPoJo.getValue();
            }
        }
        return object;
    }

    @Override
    public Object handlerRelyFormField(String corpid, FieldAttrEntity fieldAttrEntity, FieldAttrEntity targetField, LinkConditionPoJo linkConditionPoJo, LinkInfoPojo linkInfoPojo, Object value) throws XbbException {
        if (value instanceof Map) {
            Map<String, Object> map = (Map<String, Object>) value;
            value = map.getOrDefault("text", "");
        }
        if (Objects.equals(ComboTypeEnum.CUSTOM.getType(), targetField.getComboType())) {
            List<ItemPoJo> itemList = targetField.getItems();
            if (Objects.equals(linkInfoPojo.getLinkSaasMark(), SaasMarkEnum.SAAS.getCode())) {
                itemList = proFormHelp.getItem(targetField, linkInfoPojo.getLinkBusinessType(), corpid, linkInfoPojo.getLinkFormId());
            }
            Map<Object, Object> itemMap = new HashMap<>(itemList.size());
            itemList.forEach(item -> itemMap.put(item.getText(), item.getValue()));
            return itemMap.get(value);
        } else {
            return value;
        }
    }

    @Override
    public Object handlerLinkDataFixedValue(String corpid, FieldAttrEntity targetField, LinkInfoPojo linkInfoPojo, Object value) throws XbbException {
        if (value instanceof String) {
            if (Objects.equals(ComboTypeEnum.CUSTOM.getType(), targetField.getComboType())) {
                List<ItemPoJo> itemPoJos = proFormHelp.getItem(targetField, linkInfoPojo.getLinkBusinessType(), corpid, linkInfoPojo.getLinkFormId());
                targetField.setItems(itemPoJos);
                value = ItemUtil.getCustomItemValue(targetField, String.valueOf(value));
            }
        } else if (value instanceof Map || value instanceof ItemPoJo) {
            if (Objects.equals(ComboTypeEnum.CUSTOM.getType(), targetField.getComboType())) {
                ItemPoJo itemPoJo = JSON.parseObject(JSON.toJSONString(value), ItemPoJo.class);
                value = itemPoJo.getValue();
            }
        }
        return value;
    }

    @Override
    public Object handlerBusinessRelyFormField(String corpid, FieldAttrEntity fieldAttrEntity, FieldAttrEntity relyFieldEntity, LinkConditionPoJo linkConditionPoJo, Object value, Long linkFormId, Integer linkBusinessType, Integer linkSaasMark) throws XbbException {
        if (Objects.equals(ComboTypeEnum.CUSTOM.getType(), relyFieldEntity.getComboType())) {
            // 从valueMap里获取text，对比目标表单的text，拿出目标表单的value
            List<ItemPoJo> itemPoJoList = relyFieldEntity.getItems();
            if (Objects.equals(linkSaasMark, SaasMarkEnum.SAAS.getCode())) {
                itemPoJoList = proFormHelp.getItem(relyFieldEntity, linkBusinessType, corpid, linkFormId);
            }
            Map<Object, ItemPoJo> itemMap = new HashMap<>(itemPoJoList.size());
            for (ItemPoJo itemPoJo : itemPoJoList) {
                itemMap.put(itemPoJo.getText(), itemPoJo);
            }
            if (Objects.nonNull(itemMap.get(value))) {
                value = itemMap.get(value).getValue();
            }
            return value;
        } else {
            return value;
        }
    }

    @Override
    public Object handlerBusinessDataFixedValue(String corpid, FieldAttrEntity targetField, ConditionDTO conditionDTO, Object value) throws XbbException {
        if (value instanceof String) {
            if (Objects.equals(ComboTypeEnum.CUSTOM.getType(), targetField.getComboType())) {
                List<ItemPoJo> itemPoJos = proFormHelp.getItem(targetField, conditionDTO.getLinkBusinessType(), corpid, conditionDTO.getLinkFormId());
                targetField.setItems(itemPoJos);
                value = ItemUtil.getCustomItemValue(targetField, String.valueOf(value));
            }
        } else if (value instanceof Map || value instanceof ItemPoJo) {
            if (Objects.equals(ComboTypeEnum.CUSTOM.getType(), targetField.getComboType())) {
                ItemPoJo itemPoJo = JSON.parseObject(JSON.toJSONString(value), ItemPoJo.class);
                value = itemPoJo.getValue();
            }
        }
        return value;
    }

    @Override
    public Object handlerPreConditionValue(FieldAttrEntity thisField, BusinessRuleInfoPojo businessRuleInfoPojo, FilterConditionsPoJo filterCondition) throws XbbException {
        FieldAnalyseService fieldAnalyseService = fieldAnalyseMap.get(thisField.getFieldType());
        return fieldAnalyseService.handlerPreConditionValue(thisField, businessRuleInfoPojo, filterCondition);
    }
}
